﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace GenerateSpirit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DateTime start = DateTime.Now;
            /*
             * 注意：在将图片考至debug目录下时，需要手动创建old和new文件夹
             */
            string foldertype = "death";//可以是:run,stand,kill,magic--分别是：跑动，站立，物攻，法攻
            int imgCount = Directory.GetFiles(Environment.CurrentDirectory + @"\" + foldertype + "_old").Count();
            int framesPerFace = 12;
            switch (imgCount)
            {
                case 96: framesPerFace = 12;break;//跑动，八方向，各12张
                case 72: framesPerFace = 9; break;//站立，八方向，各9张
                case 88: framesPerFace = 11; break;//站立，八方向，各11张
                case 52: framesPerFace = 13; break;//物攻，4方向，各13张
                case 26: framesPerFace = 13; break;//法术
            }
            Bitmap bmpLong = new Bitmap(framesPerFace * 150, 150, PixelFormat.Format32bppArgb);
            for (int i = 1; i <= imgCount; i++)
            {
                Bitmap bmp = new Bitmap(150, 150, PixelFormat.Format32bppArgb);
                
                Graphics g = Graphics.FromImage(bmp);
                
                Image img = Bitmap.FromFile(foldertype + "_old\\" + i.ToString() + ".png");
                int w = img.Width;
                int h = img.Height;
                Rectangle srcRect = new Rectangle(0, 0, w, h);
                int left = (150 - w) / 2;
                int top = (150 - h) / 2;
                g.DrawImage(img, new Rectangle(left, top, w, h));

                bmp.Save(foldertype + "_new\\" + i.ToString() + ".png", ImageFormat.Png);

                //================================================================
                Graphics gLong = Graphics.FromImage(bmpLong);
                if (i % framesPerFace != 0)
                {
                    gLong.DrawImage(img, new Rectangle(left + (i % framesPerFace) * 150, top, w, h));
                }
                else
                {
                    gLong.DrawImage(img, new Rectangle(left + (i % framesPerFace) * 150, top, w, h));
                    bmpLong.Save(foldertype + "_new\\" + (i + 100).ToString() + ".png", ImageFormat.Png);
                    bmpLong = null;//每12帧保存一张图片，并重新生成一张新的bmp。
                    bmpLong = new Bitmap(framesPerFace * 150, 150, PixelFormat.Format32bppArgb);
                }
            }
            DateTime end = DateTime.Now;
            MessageBox.Show((end - start).TotalMilliseconds.ToString());
            this.Close();







            //Image img = Bitmap.FromFile("old\\12.png");
            //int w = img.Width;
            //int h = img.Height;
            //Rectangle srcRect = new Rectangle(0, 0, w, h);
            //int left = (150 - w) / 2;
            //int top = (150 - h) / 2;
            //g.DrawImage(img, new Rectangle(left, top, w, h));
            //System.Drawing.Imaging.ImageCodecInfo encoder = GetEncoderInfo("image/png");
            //System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(1);
            //////设置 jpeg 质量为 60
            //encoderParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,255);
            //bmp.Save("2.png", encoder, encoderParams);
        }

        private System.Drawing.Imaging.ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            //根据 mime 类型，返回编码器
            System.Drawing.Imaging.ImageCodecInfo result = null;
            System.Drawing.Imaging.ImageCodecInfo[] encoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
            for (int i = 0; i < encoders.Length; i++)
            {
                if (encoders[i].MimeType == mimeType)
                {
                    result = encoders[i];
                    break;
                }

            }
            return result;

        }


    }
}
